<template>
	<view class="goods_list" >
		<view class="goods_item" v-for="item in goods" :key="item.id">
			<image :src="item.img"></image>
			<view class="price">
				<text>{{item.price}}</text>
				<text>{{item.old_price}}</text>
			</view>
			<view class="title">{{item.title}}
			<navigator class="duihuan" url="../duihuan/duihuan" type="default" @click="exchange(item.price)">兑换</navigator></view>
			<view class="dec">{{item.dec}}</view>
		</view>
		<view class="isOver" v-if="flag">.......(≥﹏ ≤)我是有底线的.......</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				pageindex:1,
				flag:false,
				goods:[],
				integral:0,
			}
		},
		onLoad(){this.geishop()},
		methods:{
			duihuan(){
				alert('兑换成功')
			},
			async geishop(){
			const datas	=await	uni.$http.get('/cantlogin/getGoodsdata')
			if(datas.statusCode!==200) return uni.$showMsg()
			this.goods=datas.data
				},
			getData: function(){
				uni.showLoading({
					title: '数据加载中'
				});
				return new Promise((resolve, reject) => {
					uni.request({
						//api地址
						url: 'https://anmti.cn/canlogin/sinInTimes',
						//设置请求类型等于外部传入类型
						method: 'get',
						//配置请求头信息
						header: {
							"Cookie": "JSESSIONID=" + uni.getStorageSync('token').JseSsionId,
						},
						//请求成功
						success: (res) => {
							console.log(res);
							if (res.statusCode !== 200) return uni.$showMsg()
							this.integral=res.data.integral
							resolve('suc');
								uni.hideLoading();
						},
					//请求失败
						fail: (err) => {
							reject('err');
							uni.$showMsg('加载失败')
							uni.hideLoading();
						},
					})
				})
			},	
			async exchange(price){
			await this.getData()
				if(price>this.integral)
				uni.$showMsg('积分不足')
				else{
				uni.$showMsg('库存紧缺，请下次再兑换')
				}
			},
		},
		onReachBottom() {
			// console.log(this.goods.length)
			if(this.goods.length<this.pageindex*10){
				return this.flag = true
			}
			// console.log('触底了')
			this.pageindex++
			this.goods
		},
		onPullDownRefresh() {
			console.log('下拉刷新了')
		},
	}
</script>

<style lang="scss">
	.goods_list{
			padding: 0 15rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			background: #eee;
			.goods_item{
				background: #FFFFFF;
				width: 355rpx;
				margin:10rpx 0;
				padding: 15rpx;
				box-sizing: border-box;
				image{
					width: 80%;
					height: 150px;
					margin: auto;
					display: block;
				}
			}
			.price{
				color:red;
				font-weight: 600;
				font-size: 16px;
				margin: 20rpx 0 5rpx 0;
				text:nth-child(2){
					color:#ccc;
					font-size: 15px;
					margin-left: 15px;
					text-decoration: line-through;
				}
			}
			
			.title{
				font-size: 32rpx;
				font-weight: 1000;
				line-height: 50rpx;
				padding-bottom: 8rpx;
				padding-top: 5rpx;
			}
			.duihuan{
				width: 60px;
				height: 22px;
				float: right;
				background: red;
				color: white;
				text-align: center;
				line-height: 20px;
				cursor: pointer;
				font-size: 15px;
			}
			.isOver{
				width: 100%;
				height: 50px;
				line-height: 50px;
				text-align: center;
				// background: #FFFFFF;
				font-size: 25rpx;
			}
			.dec{
				font-size: 15px;
				line-height: 60rpx;
			}
			.duihuan{
				width: 60px;
				height: 22px;
				float: right;
				background: red;
				color: white;
				text-align: center;
				line-height: 20px;
				cursor: pointer;
				font-size: 15px;
				border-radius: 20%;
			}
		}
</style>
